'use strict'

define(['jquery', 'bootstrap', 'datetimepicker', 'datetimepickerCN', 'select2', 'validate', 'validate.en', 'shiftform',], function($) {
  
  return {
        
    init: function(data) {
    	this.roundId = data.roundId;
    	
        this.create();
        this.changeTime();
        this.changeCoach();
        this.editSchedule();
        this.arrangeTime();
        this.finish();
        this.cancel();
        this.edit();
        this.deleteAction();
        this.createSchedule();
        this.generateSchedule();
    },
    
    create: function() {
        $(document).on('shown.bs.modal', '#schedule-create', function(e){
            
        	$(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
	        
	        var $form = $(e.currentTarget).find("form")                          //bind shiftform
	        $form.shiftform({
	          beforeSubmit : function(el) {
	            $form.trigger("validate.ajax.submit");
	            return $.bt_validate.result[$.bt_validate.form_id]
	          }
	        })

        })
      },
      
      changeTime: function() {
          $(document).on('shown.bs.modal', '#change-time', function(e){
        	  
        	  $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
  	        
  	        var $form = $(e.currentTarget).find("form")                          //bind shiftform
  	        $form.shiftform({
  	          beforeSubmit : function(el) {
  	            $form.trigger("validate.ajax.submit");
  	            return $.bt_validate.result[$.bt_validate.form_id]
  	          }
  	        })

          })
        },
      
      changeCoach: function() {
          $(document).on('shown.bs.modal', '#change-coach', function(e){
        	  
        	  $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
  	        
  	        var $form = $(e.currentTarget).find("form")                          //bind shiftform
  	        $form.shiftform({
  	          beforeSubmit : function(el) {
  	            $form.trigger("validate.ajax.submit");
  	            return $.bt_validate.result[$.bt_validate.form_id]
  	          }
  	        })

          })
        },
        
        editSchedule: function() {
        	var that = this;
            $(document).on('shown.bs.modal', '#edit-schedule', function(e){
            	var scheduleId = $('input[name="courseScheduleId"]').val();
            	
            	$('select[name="coachId"]').on('change', function() {
            		$('.site-item').remove();
            		var coachId = $(this).val();
            		if(coachId) {
            			$.ajax({
            				type: 'POST',
            				url: window.__CTX__.root + '/individual-rounds/' + that.roundId + '/schedules/' + scheduleId + '/coach-site',
            				data: {'coachId': coachId},
            				success: function(res) {
            					res = JSON.parse(res);
            					if(res.data.siteAmount > 1) {
            						var siteSelect = '';
            						for(var index in res.data.sites) {
            							siteSelect = siteSelect + '<option value="' + res.data.sites[index].id + '">' + res.data.sites[index].name + '</option>'
            						}
            						$('.coach-item').after('<div class="form-group x-form-group site-item">' +
            								         '<label class="col-xs-3 control-label">门店</label>' + 
            								         '<div class="pull-left col-xs-8">' + 
            								           '<select name="siteId" class="form-control" data-validate="required">' +
            							                   siteSelect +
            								         '</div>' +
            									  '</div>');
            						
            						$('select[name="siteId"]').select2({
            						    placeholder : '请选择门店',
            						    minimumResultsForSearch : 5,
            						    allowClear : false,
            						  });
            					}
            				}
            			})
            		}
            	})
          	  
          	  $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
    	        
    	        var $form = $(e.currentTarget).find("form")                          //bind shiftform
    	        $form.shiftform({
    	          beforeSubmit : function(el) {
    	            $form.trigger("validate.ajax.submit");
    	            return $.bt_validate.result[$.bt_validate.form_id]
    	          }
    	        })

            })
          },
        
    arrangeTime: function(status) {
    	var that = this;
        $(document).on('shown.bs.modal', '#expiry-schedule-time', function(e){
        	
        	var scheduleId = $('input[name="scheduleId"]').val();
        	
        	$('select[name="coachId"]').on('change', function() {
        		$('.site-item').remove();
        		var coachId = $(this).val();
        		if(coachId) {
        			$.ajax({
        				type: 'POST',
        				url: window.__CTX__.root + '/individual-rounds/' + that.roundId + '/schedules/' + scheduleId + '/coach-site',
        				data: {'coachId': coachId},
        				success: function(res) {
        					res = JSON.parse(res);
        					if(res.data.siteAmount > 1) {
        						var siteSelect = '';
        						for(var index in res.data.sites) {
        							siteSelect = siteSelect + '<option value="' + res.data.sites[index].id + '">' + res.data.sites[index].name + '</option>'
        						}
        						$('.coach-item').after('<div class="form-group x-form-group site-item">' +
        								         '<label class="col-xs-3 control-label">门店</label>' + 
        								         '<div class="pull-left col-xs-8">' + 
        								           '<select name="siteId" class="form-control" data-validate="required">' +
        							                   siteSelect +
        								         '</div>' +
        									  '</div>');
        						
        						$('select[name="siteId"]').select2({
        						    placeholder : '请选择门店',
        						    minimumResultsForSearch : 5,
        						    allowClear : false,
        						  });
        					}
        				}
        			})
        		}
        	})

            $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
            
            var $form = $(e.currentTarget).find("form")                          //bind shiftform
            $form.shiftform({
              beforeSubmit : function(el) {
                $form.trigger("validate.ajax.submit");
                return $.bt_validate.result[$.bt_validate.form_id]
              }
            })
        })
      },
          
      finish: function(status) {
          $(document).on('shown.bs.modal', '#expiry-schedule-finish', function(e){

        	  $(e.currentTarget).find("form").shiftform()
          })
        },
        
      cancel: function(status) {
          $(document).on('shown.bs.modal', '#schedule-cancel', function(e){

      	    $(e.currentTarget).find("form").shiftform()
          })
        },
       
      edit: function() {
          $(document).on('shown.bs.modal', '#edit', function(e){
        	  
        	  $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
  	        
  	        var $form = $(e.currentTarget).find("form")                          //bind shiftform
  	        $form.shiftform({
  	          beforeSubmit : function(el) {
  	            $form.trigger("validate.ajax.submit");
  	            return $.bt_validate.result[$.bt_validate.form_id]
  	          }
  	        })

          })
        },
     deleteAction:function() {
          $(document).on('shown.bs.modal', '#delete-action', function(e){
        	  
        	  $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
  	        
  	        var $form = $(e.currentTarget).find("form")                          //bind shiftform
  	        $form.shiftform({
  	          beforeSubmit : function(el) {
  	            $form.trigger("validate.ajax.submit");
  	            return $.bt_validate.result[$.bt_validate.form_id]
  	          }
  	        })

          })
        },
      
      createSchedule:function() {
            $(document).on('shown.bs.modal', '#create-schedule', function(e){
          	  
          	  $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
    	        
    	        var $form = $(e.currentTarget).find("form")                          //bind shiftform
    	        $form.shiftform({
    	          beforeSubmit : function(el) {
    	            $form.trigger("validate.ajax.submit");
    	            return $.bt_validate.result[$.bt_validate.form_id]
    	          }
    	        })

            })
          },
      
     generateSchedule:function() {
              $(document).on('shown.bs.modal', '#generate-schedule', function(e){
            	  
            	  $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
      	        
      	        var $form = $(e.currentTarget).find("form")                          //bind shiftform
      	        $form.shiftform({
      	          beforeSubmit : function(el) {
      	            $form.trigger("validate.ajax.submit");
      	            return $.bt_validate.result[$.bt_validate.form_id]
      	          }
      	        })

              })
            }
  }
})
